package cn.iocoder.yudao.module.wine.service.guaranteerecord;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.guaranteerecord.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.guaranteerecord.GuaranteeRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.guaranteerecord.GuaranteeRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 保证金记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class GuaranteeRecordServiceImpl implements GuaranteeRecordService {

    @Resource
    private GuaranteeRecordMapper guaranteeRecordMapper;

    @Override
    public Long createGuaranteeRecord(GuaranteeRecordSaveReqVO createReqVO) {
        // 插入
        GuaranteeRecordDO guaranteeRecord = BeanUtils.toBean(createReqVO, GuaranteeRecordDO.class);
        guaranteeRecordMapper.insert(guaranteeRecord);
        // 返回
        return guaranteeRecord.getId();
    }

    @Override
    public void updateGuaranteeRecord(GuaranteeRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateGuaranteeRecordExists(updateReqVO.getId());
        // 更新
        GuaranteeRecordDO updateObj = BeanUtils.toBean(updateReqVO, GuaranteeRecordDO.class);
        guaranteeRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteGuaranteeRecord(Long id) {
        // 校验存在
        validateGuaranteeRecordExists(id);
        // 删除
        guaranteeRecordMapper.deleteById(id);
    }

    private void validateGuaranteeRecordExists(Long id) {
        if (guaranteeRecordMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public GuaranteeRecordDO getGuaranteeRecord(Long id) {
        return guaranteeRecordMapper.selectById(id);
    }

    @Override
    public PageResult<GuaranteeRecordRespVO> getGuaranteeRecordPage(GuaranteeRecordPageReqVO pageReqVO) {
        return guaranteeRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public GuaranteeRecordDO selectGuaranteeByNo(String no) {

        return guaranteeRecordMapper.selectOne(new LambdaQueryWrapperX<GuaranteeRecordDO>()
                .eq(GuaranteeRecordDO::getTradeNo,no));
    }

    @Override
    public GuaranteeRecordDO appGetGuaranteeRecord(Long id) {
        return guaranteeRecordMapper.selectOne(new LambdaQueryWrapperX<GuaranteeRecordDO>()
                .eq(GuaranteeRecordDO::getTeamTypeId,id)
                .eq(GuaranteeRecordDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                .orderByDesc(GuaranteeRecordDO::getCreateTime)
                .last("limit 1"));
    }

    @Override
    public List<GuaranteeRecordRespVO> appGetGuaranteeRecordList() {
        return guaranteeRecordMapper.selectJoinList(GuaranteeRecordRespVO.class,new MPJLambdaWrapperX<GuaranteeRecordDO>()
                .eq(GuaranteeRecordDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                .orderByDesc(GuaranteeRecordDO::getCreateTime)
                        .selectAll(GuaranteeRecordDO.class)
                );

    }

}
